home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / ainet / t4runtim.c < prev    next >
C/C++ Source or Header  |  1997-07-22  |  15KB  |  409 lines

  1. /* ----------------------------------------------------------------------- *
  2.  *                                                                         *
  3.  *    (C) Copyright 1997 by:  aiNet                                        *
  4.  *                                        Trubarjeva 42                                *
  5.  *                            SI-3000 Celje                                *
  6.  *                                        Europe, Slovenia                             *
  7.  *     All Rights Reserved                                                 *
  8.  *                                                                         *
  9.  *     Subject: C code for a hole in the square problem.                   *
  10.  *     File:    T4RUNTIM - Hole int the square - dynamic binding           *
  11.  *                                                                         *
  12.  * ----------------------------------------------------------------------- */
  13.  
  14. /*
  15.  * This file assumes that ainetxx.dll will be loaded at run time,
  16.  * which is default option and no flags need to be defined.
  17.  * ainetxx.lib must NOT be included in the linking process.
  18.  */
  19. #include "ainetdll.h"
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <time.h>
  24. #include <conio.h> /* This is Borland specific header.      */
  25.                    /* It is used for getch() function only. */
  26.  
  27. /*
  28.  * Path and the filename of dll which will be loaded.
  29.  */
  30.  
  31. #if defined(__WIN32__)
  32. const char ainetDll[] = "ainet32.dll";
  33. #else
  34. const char ainetDll[] = "ainet16.dll";
  35. #endif
  36. /*
  37.  * Pointers to ainet dll functions. They are made global - all functions
  38.  * can use them.
  39.  */
  40.  
  41. t_aiRegistration              aiRegistration;
  42. t_aiGetVersion                aiGetVersion;
  43. t_aiCreateModel               aiCreateModel;
  44. t_aiCreateModelFromCSVFile    aiCreateModelFromCSVFile;
  45. t_aiDeleteModel               aiDeleteModel;
  46. t_aiNormalize                 aiNormalize;
  47. t_aiDenormalize               aiDenormalize;
  48. t_aiPrediction                aiPrediction;
  49. t_aiGetNumberOfVariables      aiGetNumberOfVariables;
  50. t_aiGetNumberOfModelVectors   aiGetNumberOfModelVectors;
  51. t_aiGetNumberOfInputVariables aiGetNumberOfInputVariables;
  52. t_aiSetDiscreteFlag           aiSetDiscreteFlag;
  53. t_aiGetDiscreteFlag           aiGetDiscreteFlag;
  54. t_aiSetVariable               aiSetVariable;
  55. t_aiGetVariable               aiGetVariable;
  56. t_aiGetVariableVB             aiGetVariableVB;
  57. t_aiGetCSVFileModelSize       aiGetCSVFileModelSize;
  58. // New in version 1.24
  59. t_aiSetCapacity                    aiSetCapacity;
  60. t_aiGetCapacity                    aiGetCapacity;
  61. t_aiGetFreeEntries                aiGetFreeEntries;
  62. t_aiInsertModelVector            aiInsertModelVector;
  63. t_aiOverwriteModelVector        aiOverwriteModelVector;
  64. t_aiAppendModelVector            aiAppendModelVector;
  65. t_aiDeleteModelVector            aiDeleteModelVector;
  66. t_aiPredictionEx                    aiPredictionEx;
  67. t_aiExcludeModelVector            aiExcludeModelVector;
  68. t_aiExcludeModelVectorRange    aiExcludeModelVectorRange;
  69. t_aiIsModelVectorExcluded        aiIsModelVectorExcluded;
  70. t_aiSaveCSVFile                    aiSaveCSVFile;
  71.  
  72.  
  73. /*
  74.  *  ainet32.dll module variable.
  75.  */
  76.  
  77. HINSTANCE hLib;
  78.  
  79. /*
  80.  *  The load_aiNetLibrary() function is implemented below.
  81.  *  This function will load ainet32.dll and define pointers to
  82.  *  ainet functions.
  83.  */
  84.  
  85. int load_aiNetLibrary(void);
  86.  
  87. /*
  88.  * #########################################################################
  89.  */
  90.  
  91. void main()
  92. {
  93.     /*
  94.      * Please, see the manual (Part 1, Chapter 2.3) for more details about a
  95.      * hole in the square problem.
  96.      */
  97.  
  98.     int i;
  99.    int counter;
  100.    int loop;
  101.     int version;
  102.    int outcome;
  103.     aiModel* model;
  104.    float x,y;
  105.    int mvIndex[5];
  106.  
  107.    /* Let us define 6 vectors to be predicted.
  108.     * This definitely not enough for a good test set. However, this
  109.     * is just an example how to use aiNet.DLL library. You can
  110.     * construct a larger if you like.
  111.  
  112.     * Test points were selected near the 'border'. One half of the set
  113.     * lies slightly inside - in the hole, and the other half of the
  114.     * set lies slightly outside - in the square.
  115.     *       +-------+--------+-------+-------+--------+
  116.     *       |   Fi  |   R    |   X   |   Y   | IN/OUT |
  117.     *       +-------+--------+-------+-------+--------+
  118.     *    1  |    0  |  0.75  | 0.750 | 0.000 |  OUT   |
  119.     *    2  |   60  |  0.65  | 0.325 | 0.563 |  IN    |
  120.     *    3  |  120  |  0.75  |-0.375 | 0.650 |  OUT   |
  121.     *    4  |  180  |  0.65  |-0.650 | 0.000 |  IN    |
  122.     *    5  |  240  |  0.75  |-0.375 |-0.650 |  OUT   |
  123.     *    6  |  300  |  0.65  | 0.325 |-0.563 |  IN    |
  124.     *       +-------+--------+-------+-------+--------+
  125.     */
  126.     float predict[6][3] = { { 0.750, 0.000, 999 },  /* vectors to be predicted */
  127.                                     { 0.325, 0.563, 999 },
  128.                                     {-0.375, 0.650, 999 },
  129.                                     {-0.650, 0.000, 999 },
  130.                                     {-0.375,-0.650, 999 },
  131.                                     { 0.325,-0.563, 999 } };
  132.    /*
  133.     * Load DLL
  134.     */
  135.    if( !load_aiNetLibrary() ) {
  136.         exit(EXIT_FAILURE);
  137.     }
  138.    
  139.     /*
  140.      * Print out the title
  141.      */
  142.     version = aiGetVersion();
  143.     printf( "\naiNetDLL version %i.%i! (C) Copyright by aiNet, 1997",
  144.               version/100, version%100 );
  145.     printf( "\n---------------------------------------------------\n" );
  146.  
  147.     /*
  148.      * Register DLL
  149.      */
  150.     aiRegistration( "Your registration name", "Your code" );
  151.  
  152.     /*
  153.      * Setup the model. We will start with 10 model vectors. They will be
  154.     * generated using random number generator.
  155.      */
  156.     model = aiCreateModel(10,3,2);
  157.     if(!model) {
  158.         printf( "\nError: Something went wrong during model creation!" );
  159.         exit(EXIT_FAILURE);
  160.     }
  161.  
  162.    /* Let us generate initial model vectors */
  163.    randomize();
  164.    for(i=1; i<=10; i++) {
  165.        float inside;
  166.        x = (float)((random(2000)-1000))/1000.0;
  167.        y = (float)((random(2000)-1000))/1000.0;
  168.  
  169.       if ( (x*x + y*y - 0.490) <= 0.0 )
  170.           inside = 1.0;
  171.        else
  172.           inside = 0.0;
  173.  
  174.       aiSetVariable(model,i,1,x);
  175.       aiSetVariable(model,i,2,y);
  176.       aiSetVariable(model,i,3,inside);
  177.    }
  178.  
  179.     /*
  180.      * Output the model
  181.      */
  182.  
  183.     printf( "\n             Model name: aiNet DLL test 4 (Hole in the square)" );
  184.     printf( "\nNumber of model vectors: %i", aiGetNumberOfModelVectors(model));
  185.     printf( "\n    Number of variables: %i", aiGetNumberOfVariables(model));
  186.     printf( "\n         Variable names: X,   Y,   IN(1)/OUT(0)" );
  187.     printf( "\n          Discrete flag: %i,   %i,   %i",
  188.               aiGetDiscreteFlag(model,1),
  189.               aiGetDiscreteFlag(model,2),
  190.               aiGetDiscreteFlag(model,3) );
  191.     for( i=1; i<=aiGetNumberOfModelVectors(model); i++ ) {
  192.         printf( "\n\t\t\t % 5.3f, % 5.3f, % 5.3f",
  193.                   aiGetVariable(model, i,1),
  194.                   aiGetVariable(model, i,2),
  195.                   aiGetVariable(model, i,3) );
  196.     }
  197.  
  198.     /* This loop will be repeated 7 times                        */
  199.    /* Each time new model vectors will be added to the problem. */
  200.  
  201.     for(loop=0; loop<7; loop++) {
  202.        int oldSize;
  203.         /*
  204.          * Normalize the model
  205.          */
  206.         aiNormalize(model,NORMALIZE_REGULAR);
  207.  
  208.         /*
  209.          * Prediction: Pen. coefficient = 0.30, Pen. method = STATIC
  210.          * This test has static penalty coefficient 0.30
  211.          */
  212.  
  213.         printf( "\n\n  Penalty coefficient: 0.15" );
  214.         printf(   "\n       Penalty method: STATIC" );
  215.         printf(   "\n\t X(inp), Y(inp), IN/OUT (out)" );
  216.         counter = 0;
  217.         for ( i=0; i<6; i++ ) {
  218.           int inside;
  219.          int j;
  220.          /* TRUE stands for most influent MV */
  221.             aiPredictionEx( model, predict[i], 0.15, PENALTY_STATIC, mvIndex, 5, TRUE );
  222.             printf( "\n    % 7.4f, % 7.4f, % 7.4f",
  223.                       predict[i][0],predict[i][1],predict[i][2] );
  224.          inside = predict[i][2] >= 0.5;
  225.           if( inside )
  226.               printf( " IN " );
  227.           else
  228.               printf( " OUT" );
  229.           if( inside == i%2 ) {
  230.               printf( " PASSED" );
  231.              counter++;
  232.           }
  233.           else {
  234.               printf( " FAILED" );
  235.           }
  236.          printf( " Most inf. MVs: " );
  237.          for( j=0; j<5; j++ ) {
  238.              printf( "%i", mvIndex[j] );
  239.             if(j!=4) printf(", ");
  240.          }
  241.         }
  242.        printf( "\n\t%i Correct predictions (%3.2f)!", counter, counter/6.0*100.0 );
  243.       printf( "\nPress any key to continue!" );
  244.        getch();
  245.  
  246.         /*
  247.          * Denormalize the model!
  248.         * We must do that, because ne